const {
  cartAddErr,
  cartUpdateErr,
  cartRemoveErr,
  cartBulkUpdateErr,
  cartGetListErr
} = require('@constant/error.type');

const { controller, emit } = require('@utils/tools');

const {
  cartAdd,
  cartGetList,
  cartUpdate,
  cartRemove,
  cartBulkUpdate,
} = require('./cart.service');

module.exports = {
  async add(ctx) {
    const params = {
      user_id: ctx.state.user.id,
      goods_id: ctx.request.body.goods_id,
    };

    const res = await controller(params, cartAdd);

    emit(ctx, res, res, cartAddErr);
  },

  async list(ctx) {
    const params = {
      ...ctx.query,
      user: ctx.state.user,
    };

    const res = await controller(params, cartGetList);

    emit(ctx, res, res, cartGetListErr);
  },

  async update(ctx) {
    const res = await controller(ctx.request.body, cartUpdate);

    emit(ctx, res, res, cartUpdateErr);
  },

  async remove(ctx) {
    const params = { id: ctx.request.params.id };

    const res = await controller(params, cartRemove);

    emit(ctx, res, params, cartRemoveErr);
  },

  async bulkUpdate(ctx) {
    const { selection, ids } = ctx.request.body;

    const params = {
      selection,
      ids,
    };

    const res = await controller(params, cartBulkUpdate);

    emit(ctx, res, params, cartBulkUpdateErr);
  },
};
